package com.heima.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.apis.article.IArticleClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.StatisticsDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.DateUtils;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmNewsService;
import com.heima.wemedia.service.WmStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
@Transactional
public class WmStatisticsServiceImpl implements WmStatisticsService {
    @Autowired
    private WmNewsService wmNewsService;
    @Autowired
    private IArticleClient articleClient;
    @Autowired
    private WmUserMapper wmUserMapper;

    /**
     * 图文统计
     * @param beginDate
     * @param endDate
     * @return
     */
    @Override
    public ResponseResult newsDimension(String beginDate, String endDate) {
        //1.创建返回类
        Map<String,Object> resultMap=new HashMap<>();
        //2.将时间转换类型
        Date beginDateTime = DateUtils.stringToDate(beginDate);
        Date endDateTime = DateUtils.stringToDate(endDate);
        //3.获取用户
        WmUser user = WmThreadLocalUtil.getUser();
        WmUser wmUser = wmUserMapper.selectById(user.getId());
        Integer apUserId = wmUser.getApUserId();

        //4.图文发布--查询该账号发布过多少文章
        LambdaQueryWrapper<WmNews> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(WmNews::getUserId,user.getId())
                .eq(WmNews::getStatus,WmNews.Status.PUBLISHED.getCode())
                .eq(WmNews::getEnable,1)
                .between(WmNews::getPublishTime,beginDateTime,endDateTime);
        int count = wmNewsService.count(queryWrapper);
        resultMap.put("publishNum",count);
        //5.点赞数与收藏数
        ResponseResult responseResult = articleClient.queryLikesAndConllections(apUserId, beginDateTime, endDateTime);
        if (responseResult.getCode().equals(200)){
            String mapJson = JSON.toJSONString(responseResult.getData());
            Map map = JSON.parseObject(mapJson, Map.class);
            resultMap.put("likesNum", map.get("likes") == null ? 0 : map.get("likes"));
            resultMap.put("collectNum", map.get("collections") == null ? 0 : map.get("collections"));
        }
        return ResponseResult.okResult(resultMap);
    }

    /**
     * 分页查询图文统计
     * @param dto
     * @return
     */
    @Override
    public PageResponseResult newsPage(StatisticsDto dto) {
        //1.检查分页
        dto.checkParam();
        WmUser user = WmThreadLocalUtil.getUser();
        WmUser wmUser = wmUserMapper.selectById(user.getId());
        Integer apUserId = wmUser.getApUserId();
        dto.setApUserId(apUserId);
        PageResponseResult responseResult = articleClient.newPage(dto);
        return responseResult;
    }
}
